package ejb;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import model.Mycomment;
import model.Myuser;

@Stateless
public class CommentInspect implements CommentInspectRemote{
	@PersistenceContext
	EntityManager em;
		
	@Override
	public List<Mycomment> getAllComments(){
		String sql="select u from Mycomment u";
    	Query query= em.createQuery(sql);
    	List<Mycomment> commentsList = (List<Mycomment>) query.getResultList();
    	return commentsList;
	}

	@Override
	public void addComment(Mycomment comment) {
//		Myuser user=new Myuser();
//		user.setUsername(username);
//		user.setPassword(password);
//		user.setAuthority("personal user");
//		em.persist(user);
		em.persist(comment);
    }

	public Mycomment getCommentById(int id) {
		String sql = "select u from Mycomment u where u.commentid=:d";
		Query query = em.createQuery(sql);
		query.setParameter("d", id);
		List<Mycomment> commentList = (List<Mycomment>) query.getResultList();
		if(commentList!=null){
			return commentList.get(0);
		}
		return null;
	}
	
}
